/**
 * 树表格
 */
Ext.define('BenBen.common.BaseTreeGrid', {
    extend: 'Ext.tree.Panel',
    requires: [
		'Ext.ux.CheckColumn',
		'BenBen.common.FormWindow'
    ],
    border : false,
    bodyStyle : 'padding:5px',
    height: 540,
    collapsible: true,
    useArrows: true,
    rootVisible: false,
    multiSelect: true,
    singleExpand: true,
    isLoad : false,
    tbarDefaultItems : "add,update,delete,look,search",
    isDistribute : false, //是否为分配使用
    initComponent: function () {
    	var me = this;
    	
    	var tb  = Ext.create('Ext.toolbar.Toolbar');
    	

		var defalutItems = me.tbarDefaultItems;
		if (defalutItems.indexOf('add') != -1) {
			tb.add({
				text : '新增',
				iconCls : 'gridadd',
				handler : Ext.Function.bind(me.addFn, me)
			});
		}

		if (defalutItems.indexOf('update') != -1) {
			tb.add({
				text : '修改',
				iconCls : 'gridupdate',
				handler : Ext.Function.bind(me.updateFn, me)
			});
		}

		if (defalutItems.indexOf('look') != -1) {
			tb.add({
				text : '查看',
				iconCls : 'gridlook',
				handler : Ext.Function.bind(me.showFn, me)
			});
		}

		if (defalutItems.indexOf('delete') != -1) {
			tb.add({
				text : '删除',
				iconCls : 'griddelete',
				handler : Ext.Function.bind(me.deleteFn, me)
			});
		}

		if (defalutItems.indexOf('search') != -1) {
			tb.add({
				xtype : 'searchfield',
				name : 'name',
				store : me.store,
				hiddenLabel : true,
				width : 200,
				emptyText : '输入查询条件',
				value : ''
			});
		}
		if (me.tbarItems) {
			for ( var i = 0; i < me.tbarItems.length; i++) {
				tb.add(me.tbarItems[i]);
			}
		}
       
        Ext.apply(this,{
            border : false,
            tbar : tb,
            store: me.store,
            config : me.config,
            columns: me.columns
        });
        
        
        me.on("itemclick",function(view,record,item,index,e){
    		me.dataId = record.get('id');
        });

        this.callParent(arguments);
    },
    createEditWindow : function(type,url,id){
        var me = this;
        var config = this.config;
        if(type == 1){
            me.dataForm.getForm().reset();
        }

        var window = Ext.create('BenBen.common.FormWindow', {
            width : config.winWidth,
            height : config.winHeigth,
            dataForm : me.dataForm,
            config : {
                type : type,
                url : url,
                store : me.getStore(),
                recordId : id
            }
        });

        return window;
    },
	addFn : function() {
        var url = this.config.url;
		this.createEditWindow(1,url,null).show();
	},
	updateFn : function() {
        if(this.dataId){
            var url = this.config.url;

            url += '/' + this.dataId;

            var window = this.createEditWindow(2,url,this.dataId);

            this.loadFormData(url);
            window.show();
		}
	},
	showFn : function() {
	  if(this.dataId){
            var url = this.config.url;

            url += '/' + this.dataId;

            var window = this.createEditWindow(3 ,url,this.dataId);

            this.loadFormData(url);
            window.show();
        }
	},
	deleteFn : function() {
		var me = this;
		 if(me.dataId){
	        var url = this.config.url;
	        url += '/' + me.dataId;
    		if(this.getSelectionModel() != null){
    			var record = me.getSelectionModel().getSelection()[0];
    			var store = me.store;
    			Ext.Ajax.request({
                    url: url,
                    method: 'DELETE',
                    callback: function (options, success, response) {
                        if (success) {
                            if (Ext.decode(response.responseText).success) {
                            	Ext.Msg.confirm('成功','删除数据成功');
    						} else {
    							Ext.Msg.confirm('失败',Ext.decode(response.responseText).info);
    						}
                            me.reload();
                        } else {
                            Ext.Msg.confirm('失败','删除数据失败');
                        }
                    }
                });
    		}
        }
	},
    getStore : function(){
        return this.store;
    },
    reload : function(){
        this.getStore().load();
    },
	loadFormData : function(url){
    	var me =  this.dataForm;
    	me.getForm().load({
            url : url,
            method:'GET',
            waitMsg : '加载...',
			success : function(form, action) {
				
			}
        });
    },
    listeners: {
        checkchange: function(node, state) { 
        //如果被勾选的节点有子节点，则将其子节点全部改为根节点状态
          if (node.hasChildNodes()) {
            for (var j = 0; j < node.childNodes.length; j++) {
                node.childNodes[j].set('checked', state);
            }
          }
          
          var parent = node.parentNode;
          if(state){
        	  parent.set('checked', state);
          }
        }
    }
});
